package routes

import (
	"navigation-service/internal/handlers"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
)

// 设置路由
func SetupRoutes(r *gin.Engine) {
	// 配置CORS
	r.Use(cors.New(cors.Config{
		AllowOrigins:     []string{"*"},
		AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		AllowHeaders:     []string{"*"},
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
	}))

	// 静态文件服务
	r.Static("/static", "./static")
	r.LoadHTMLGlob("templates/*")

	// 页面路由
	r.GET("/", handlers.IndexHandler)
	r.GET("/import", handlers.ImportHandler)
	r.GET("/test-extension", handlers.TestExtensionHandler)
	r.GET("/stat", handlers.StatsHandler)
	r.GET("/ai-grouping", handlers.AIGroupingHandler)
	r.GET("/download", handlers.DownloadHandler)
	r.GET("/test-grouping", func(c *gin.Context) {
		c.File("test-grouping.html")
	})

	// API路由
	api := r.Group("/api")
	{
		// 导航数据相关
		api.GET("/navigation", handlers.GetNavigationData)
		api.GET("/active-grouping", handlers.GetActiveGrouping)
		api.GET("/categories", handlers.GetCategories)
		api.GET("/categories/:id", handlers.GetCategoryByID)
		api.GET("/search", handlers.SearchItems)

		// 收藏相关
		api.POST("/favorites", handlers.AddFavorite)
		api.DELETE("/favorites/:name", handlers.RemoveFavorite)
		api.GET("/favorites", handlers.GetFavorites)

		// 最近使用
		api.POST("/recently-used", handlers.AddRecentlyUsed)

		// 统计信息
		api.GET("/stats", handlers.GetStats)

		// 浏览数据相关
		api.POST("/browsing-history", handlers.AddBrowsingHistory)
		api.GET("/browsing-stats", handlers.GetBrowsingStats)
		api.GET("/browsing-records", handlers.GetBrowsingRecords)

		// 书签相关
		api.POST("/bookmarks", handlers.ImportBookmarks)
		api.GET("/bookmarks", handlers.GetBookmarks)

		// 手动导入浏览历史
		api.POST("/import-history", handlers.ImportBrowsingHistory)

		// AI分组相关
		api.GET("/ai-grouping/versions", handlers.GetAIGroupingVersions)
		api.POST("/ai-grouping/versions", handlers.CreateAIGroupingVersion)
		api.GET("/ai-grouping/versions/:id", handlers.GetAIGroupingVersion)
		api.PUT("/ai-grouping/versions/:id", handlers.UpdateAIGroupingVersion)
		api.DELETE("/ai-grouping/versions/:id", handlers.DeleteAIGroupingVersion)

		// AI分组管理
		api.POST("/ai-grouping/groups", handlers.CreateAIGroup)
		api.GET("/ai-grouping/groups", handlers.GetAIGroups)
		api.PUT("/ai-grouping/groups/:id", handlers.UpdateAIGroup)
		api.DELETE("/ai-grouping/groups/:id", handlers.DeleteAIGroup)

		// URL分组分配
		api.GET("/ai-grouping/assignments", handlers.GetURLGroupAssignments)
		api.POST("/ai-grouping/assignments", handlers.CreateURLGroupAssignment)
		api.PUT("/ai-grouping/assignments/:id", handlers.UpdateURLGroupAssignment)
		api.DELETE("/ai-grouping/assignments/:id", handlers.DeleteURLGroupAssignment)

		// AI分组任务
		api.POST("/ai-grouping/tasks", handlers.CreateAIGroupingTask)
		api.GET("/ai-grouping/tasks/:versionId", handlers.GetAIGroupingTask)

		// AI分组分析
		api.GET("/ai-grouping/patterns", handlers.AnalyzeBrowsingPatterns)
		api.POST("/ai-grouping/test", handlers.TestGroupingAlgorithm)
		api.POST("/ai-grouping/execute", handlers.ExecuteAIGrouping)
		api.POST("/ai-grouping/use-grouping", handlers.UseGrouping)
		api.DELETE("/ai-grouping/versions/:id/clear", handlers.ClearAIGroups)
		api.DELETE("/ai-grouping/clear-all", handlers.ClearAllAIGroups)
		api.DELETE("/ai-grouping/delete-all", handlers.DeleteAllAIGroupsAndData)

		// 下载相关
		api.GET("/download/:platform", handlers.DownloadFile)
		api.GET("/download/check/:platform", handlers.CheckDownloadFile)
	}
}
